package com.shouhuobao.station.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import droid.frame.utils.android.Alert;

/**
 * 微信支付回调接口
 *
 * @author coffee<br>
 * 2015-10-21下午8:46:34
 */
public class WXPayEntryActivity extends BaseActivity2 implements IWXAPIEventHandler {
    private IWXAPI api;
    /**
     * 0,包裹收银台 、1，邮票收银台  11 驿站开通支付  21 火箭侠注册开通支付
     */
    public static int payFrom = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        cancelLoadingDialog();
        PayResp respNew = (PayResp) resp;
        switch (resp.errCode) {
            case 0:
                if (respNew.extData == null) {
                    Alert.toast("支付成功");
                    if (payFrom == 0) {
                        String orderId = AppCache.getOrderId();
                        // 微信支付回调成功后回到首页
                        AppHttp.getInstance().orderPayCallBack(orderId, 1, 0);
                        AppLogic.onPaySuccess(getContext());
                    } else if (payFrom == 11) {//驿站开通支付成功回调
                        Intent intent = new Intent();
                        intent.setAction("station_open_wxpay_callback");
                        sendBroadcast(intent);
                    } else if (payFrom == 21) {
                        Intent intent = new Intent();
                        intent.setAction("courier_open_wxpay_callback");
                        sendBroadcast(intent);
                    } else {
                        Intent intent = new Intent();
                        intent.setAction("wxpay_callback");
                        sendBroadcast(intent);
                    }
                    payFrom = 0;
                }
                break;
            default:
                Alert.toast("系统异常" + resp.errCode);
                break;
            case -2:
                Alert.toast("支付已取消");
                break;
        }
        finish();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.wxPayCallback:
                ReqResult<?> result1 = JSON.parser(msg.obj);
                switch (result1.getStatus()) {
                    case 1:
                        // 微信支付回调成功后回到首页
                        break;
                    default:
                        // showToast(result1.getMessage());
                        break;
                }
                break;
            default:
                break;
        }
        return super.handleMessage(msg);
    }

}
